/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import commonpackage.Eating;
import commonpackage.Hosting;
import commonpackage.Order;
import commonpackage.POI;
import commonpackage.Position;
import commonpackage.Travel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

/**
 *
 * @author chuda
 */
public class Helper {
    /*ERRORS*/

    public static String NO_SUCH_POI = "Sorry, there's no such a POI in database...";
    public static String NO_SPACE_RESTAURANT = "Sorry, there's no more space in restaurant...";
    public static String NO_ROOMS_HOTEL = "Sorry, there's no more available rooms in hotel...";
    public static String UNKNOWN_ERROR = "There was an error making an order...";
    public static String IMPOSSIBLE_ORDER = "It's not possible to order this POI!";
    public static String ORDER_CANCEL_SUCCESS = "Order cancelled success!";

    /*CLASSIFICATION OF STUFF*/
    public static String EATING = "eating";
    public static String HOSTING = "hosting";
    public static String TRAVEL = "travel";
    /*Database columns names*/
    /*POIS*/
    public static String POIS = "POIS";
    public static String POIID = "POIID";
    public static String DESCRIPTION = "description";
    public static String NAME = "name";
    public static String URL = "img_url";
    public static String CLASSIFICATION = "classification";
    public static String TELEPHONE = "telephone";
    public static String ADDRESS = "ADDRESS";
    public static String TICKET = "ticket";
    public static String LATITUDE = "latitude";
    public static String LONGITUDE = "longitude";
    public static String TYPE = "type";
    public static String MIN_PRICE = "min_price";
    public static String MAX_PRICE = "max_price";
    public static String AVG_PRICE = "avg_price";
    public static String ACCOMODATION = "accomodation";
    public static String COUNTRY = "country";
    public static String CITY = "city";
    public static String PRICE = "price";
    public static String MIN_PRICE_NIGHT = "min_price_night";
    public static String MAX_PRICE_NIGHT = "max_price_night";
    public static String NUM_AVAILABLE_ROOMS = "num_available_rooms";
    public static String LUNCH = "lunch_included";

    /*ORDERS*/
    public static String ORDERS = "orders";
    public static String ORDERID = "orderid";
    public static String MOBILENUM = "mobilenumber";
    public static String NUM_PEOPLE = "num_people";
    public static String DATE = "DATE";
    public static String NAME_ORDERER = "name_orderer";

    /*TAGS*/
    public static String TAGS = "TAGS";
    public static String TAG = "tag";

    /*POI_TAG*/
    public static String POI_TAGS = "POI_TAGS";

//description     VARCHAR(128),
//  classification VARCHAR(32),
//  telephone       VARCHAR(32),
//  address         VARCHAR(64),
//  ticket          BOOL,
//  latitude        FLOAT,
//  longitude       FLOAT,
//  type         VARCHAR(32),
//  min_price       FLOAT,
//  max_price       FLOAT,
//  avg_price       FLOAT,
//  accomodation    INT,
//  country     VARCHAR(32),
//  city        VARCHAR(32),
//  price       FLOAT,
//
//  min_price_night     FLOAT,
//  max_price_night     FLOAT,
//  num_available_rooms INT,
//  lunch_included      BOOL,
    /*Constructs POI object from ResultSet object*/
    public static POI constructPOIFromRS(ResultSet rs) {
        POI poi = null;
        int id;
        String description = "";
        String name = "";
        String classification = "";
        String telephone = "";
        String address = "";
        double latitude = 0;
        double longitude = 0;
        boolean ticket = false;
        String type = "";
        double min_price = 0;
        double max_price = 0;
        double avg_price = 0;
        int accomodation = 0;
        String country = "";
        String city = "";
        double price = 0;
        double min_price_night = 0;
        double max_price_night = 0;
        int num_available_rooms = 0;
        boolean lunch_included = false;
        String url = "";
        try {
            //String img_url = "";
            id = rs.getInt(Helper.POIID);
            name = rs.getString(Helper.NAME);
            description = rs.getString(Helper.DESCRIPTION);
            classification = rs.getString(Helper.CLASSIFICATION);
            telephone = rs.getString(Helper.TELEPHONE);
            address = rs.getString(Helper.ADDRESS);
            latitude = rs.getFloat(Helper.LATITUDE);
            longitude = rs.getFloat(Helper.LONGITUDE);
            ticket = rs.getBoolean(Helper.TICKET);
            type = rs.getString(Helper.TYPE);
            min_price = rs.getFloat(Helper.MIN_PRICE);
            max_price = rs.getFloat(Helper.MAX_PRICE);
            avg_price = rs.getFloat(Helper.AVG_PRICE);
            accomodation = rs.getInt(Helper.ACCOMODATION);
            country = rs.getString(Helper.COUNTRY);
            city = rs.getString(Helper.CITY);
            price = rs.getFloat(Helper.PRICE);
            min_price_night = rs.getFloat(Helper.MIN_PRICE_NIGHT);
            max_price_night = rs.getFloat(Helper.MAX_PRICE_NIGHT);
            lunch_included = rs.getBoolean(Helper.LUNCH);
            num_available_rooms = rs.getInt(Helper.NUM_AVAILABLE_ROOMS);
            url = rs.getString(Helper.URL);
            // public Eating (String type,String descr,boolean order, Position pos, double min_price, double max_price,
            // double avg_price, int accomodation)
            //  public Travel (String type,String descr,boolean order, Position pos, String country, String city, double price)
            // public Hosting (String type,String descr,boolean order, Position pos,
            //  double min_price_n,double max_price_n,int num_av_r,boolean lunch)
            if (classification.equalsIgnoreCase(Helper.EATING)) {
                Eating eating = new Eating(type, name, description, address, telephone, ticket, new Position(longitude, latitude), min_price, max_price,
                        avg_price, accomodation);
                eating.setId(id);
                eating.setUrl(url);
                poi = eating;
            } else if (classification.equalsIgnoreCase(Helper.TRAVEL)) {
                Travel travel = new Travel(type, name, description, address, telephone, ticket, new Position(longitude, latitude), country, city, price);
                travel.setId(id);
                travel.setUrl(url);
                poi = travel;
            } else if (classification.equalsIgnoreCase(Helper.HOSTING)) {
                Hosting hosting = new Hosting(type, name, description, address, telephone, ticket, new Position(longitude, latitude),
                        min_price_night, max_price_night, num_available_rooms, lunch_included);
                hosting.setUrl(url);
                hosting.setId(id);
                poi = hosting;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return poi;
    }

    /*
     * Converts result set into array list of POIs
     **/
    public static String[] convertResultSetIntoPOIS(ResultSet rs) {
        String[] pois = new String[15];
        int i = 0;
        POI p;
        try {

            while (rs.next()) {
                p = constructPOIFromRS(rs);
                pois[i] = p.toStringX();
                i++;
            }


        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return pois;
    }

    /*
     * Converts result set into array list of POIs adding a distance from position
     **/
    public static String[] convertResultSetIntoPOIS(ResultSet rs, Position pos) {
        String[] pois = new String[15];
        int i = 0;
        POI p;
        try {

            while (rs.next()) {
                p = constructPOIFromRS(rs);
                pois[i] = p.toString2(pos);
                i++;
            }


        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return pois;
    }

    /*
     * Converts result set into array list of eatings
     **/
    public static String[] convertResultSetIntoEating(ResultSet rs) {
        String[] pois = new String[15];
        int i = 0;
        Eating e;
        try {

            while (rs.next()) {
                e = constructEatingFromRS(rs);
                if (e != null) {
                    pois[i] = e.toStringX();
                    i++;
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return pois;
    }

    /*
     * converts result set into arraylist of hostings
     **/
    public static String[] convertResultSetIntoHosting(ResultSet rs) {
        String[] pois = new String[15];
        int i = 0;
        Hosting h;
        try {

            while (rs.next()) {
                h = constructHostingFromRS(rs);
                pois[i] = h.toStringX();
                i++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return pois;
    }

    /*
     * converts result set into array list of travels
     **/
    public static String[] convertResultSetIntoTravel(ResultSet rs) {
        String[] pois = new String[15];
        int i = 0;
        Travel t;
        try {

            while (rs.next()) {
                t = constructTravelFromRS(rs);
                pois[i] = t.toStringX();
                i++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return pois;
    }

    /*converts result set into array list of orders */
    public static String[] convertResultSetIntoOrders(ResultSet rs) {
        String[] orders = new String[30];
        POI poi;
        String telNumber = null;
        Date date = null;
        int numPeople = 1;
        String nameOrderer = null;
        int poiid;
        int orderid;
        int i = 0;
        try {
            while (rs.next()) {
                telNumber = rs.getString(Helper.MOBILENUM);
                date = rs.getTimestamp(Helper.DATE);
                System.out.println("DATE in DB: " + date.toString());
                numPeople = rs.getInt(Helper.NUM_PEOPLE);
                nameOrderer = rs.getString(Helper.NAME_ORDERER);
                poiid = rs.getInt(Helper.POIID);
                poi = new DBManagement().selectPOIByID(poiid);
                orderid = rs.getInt(Helper.ORDERID);
                Order order = new Order(poi, telNumber, numPeople, nameOrderer);
                order.setResDate(date);
                order.setOrderid(orderid);
                orders[i] = order.toStringX();
                i++;
            }
        } catch (SQLException ex) {
            System.out.println("bang... e a sujidade desaparece");
            ex.printStackTrace();
        }
        return orders;
    }

    /*Constructs Eating object from ResultSet object*/
    public static Eating constructEatingFromRS(ResultSet rs) {
        Eating poi = null;
        int id;
        String name = "";
        String description = "";
        String classification = "";
        String telephone = "";
        String address = "";
        double latitude = 0;
        double longitude = 0;
        boolean ticket = false;
        String type = "";
        double min_price = 0;
        double max_price = 0;
        double avg_price = 0;
        int accomodation = 0;
        String country = "";
        String city = "";
        double price = 0;
        double min_price_night = 0;
        double max_price_night = 0;
        int num_available_rooms = 0;
        boolean lunch_included = false;
        String url = "";
        try {
            //String img_url = "";
            id = rs.getInt(Helper.POIID);
            description = rs.getString(Helper.DESCRIPTION);
            name = rs.getString(Helper.NAME);
            classification = rs.getString(Helper.CLASSIFICATION);
            telephone = rs.getString(Helper.TELEPHONE);
            address = rs.getString(Helper.ADDRESS);
            latitude = rs.getFloat(Helper.LATITUDE);
            longitude = rs.getFloat(Helper.LONGITUDE);
            ticket = rs.getBoolean(Helper.TICKET);
            type = rs.getString(Helper.TYPE);
            min_price = rs.getFloat(Helper.MIN_PRICE);
            max_price = rs.getFloat(Helper.MAX_PRICE);
            avg_price = rs.getFloat(Helper.AVG_PRICE);
            accomodation = rs.getInt(Helper.ACCOMODATION);
            country = rs.getString(Helper.COUNTRY);
            city = rs.getString(Helper.CITY);
            price = rs.getFloat(Helper.PRICE);
            min_price_night = rs.getFloat(Helper.MIN_PRICE_NIGHT);
            max_price_night = rs.getFloat(Helper.MAX_PRICE_NIGHT);
            lunch_included = rs.getBoolean(Helper.LUNCH);
            num_available_rooms = rs.getInt(Helper.NUM_AVAILABLE_ROOMS);
            url = rs.getString(Helper.URL);
            // public Eating (String type,String descr,boolean order, Position pos, double min_price, double max_price,
            // double avg_price, int accomodation)
            //  public Travel (String type,String descr,boolean order, Position pos, String country, String city, double price)
            // public Hosting (String type,String descr,boolean order, Position pos,
            //  double min_price_n,double max_price_n,int num_av_r,boolean lunch)
            System.out.println(name);
            if (classification.equalsIgnoreCase(Helper.EATING)) {
                Eating eating = new Eating(type, name, description, address, telephone, ticket, new Position(longitude, latitude), min_price, max_price,
                        avg_price, accomodation);
                eating.setUrl(url);
                eating.setId(id);
                poi = eating;
            } else {
                System.out.println("Fodeu-se 0.9");
                return null;
            }
        } catch (SQLException ex) {
            System.out.println("FUDEU_SE");
            ex.printStackTrace();
        }

        return poi;
    }

    /*Constructs Hosting object from ResultSet object*/
    public static Hosting constructHostingFromRS(ResultSet rs) {
        Hosting poi = null;
        int id;
        String description = "";
        String name = "";
        String classification = "";
        String telephone = "";
        String address = "";
        double latitude = 0;
        double longitude = 0;
        boolean ticket = false;
        String type = "";
        double min_price = 0;
        double max_price = 0;
        double avg_price = 0;
        int accomodation = 0;
        String country = "";
        String city = "";
        double price = 0;
        double min_price_night = 0;
        double max_price_night = 0;
        int num_available_rooms = 0;
        boolean lunch_included = false;
        String url = "";
        try {
            //String img_url = "";
            id = rs.getInt(Helper.POIID);
            name = rs.getString(Helper.NAME);
            description = rs.getString(Helper.DESCRIPTION);
            classification = rs.getString(Helper.CLASSIFICATION);
            telephone = rs.getString(Helper.TELEPHONE);
            address = rs.getString(Helper.ADDRESS);
            latitude = rs.getFloat(Helper.LATITUDE);
            longitude = rs.getFloat(Helper.LONGITUDE);
            ticket = rs.getBoolean(Helper.TICKET);
            type = rs.getString(Helper.TYPE);
            min_price = rs.getFloat(Helper.MIN_PRICE);
            max_price = rs.getFloat(Helper.MAX_PRICE);
            avg_price = rs.getFloat(Helper.AVG_PRICE);
            accomodation = rs.getInt(Helper.ACCOMODATION);
            country = rs.getString(Helper.COUNTRY);
            city = rs.getString(Helper.CITY);
            price = rs.getFloat(Helper.PRICE);
            min_price_night = rs.getFloat(Helper.MIN_PRICE_NIGHT);
            max_price_night = rs.getFloat(Helper.MAX_PRICE_NIGHT);
            lunch_included = rs.getBoolean(Helper.LUNCH);
            num_available_rooms = rs.getInt(Helper.NUM_AVAILABLE_ROOMS);
            url = rs.getString(Helper.URL);
            // public Eating (String type,String descr,boolean order, Position pos, double min_price, double max_price,
            // double avg_price, int accomodation)
            //  public Travel (String type,String descr,boolean order, Position pos, String country, String city, double price)
            // public Hosting (String type,String descr,boolean order, Position pos,
            //  double min_price_n,double max_price_n,int num_av_r,boolean lunch)

            if (classification.equalsIgnoreCase(Helper.HOSTING)) {
                Hosting hosting = new Hosting(type, name, description, address, telephone, ticket, new Position(longitude, latitude),
                        min_price_night, max_price_night, num_available_rooms, lunch_included);
                hosting.setUrl(url);
                hosting.setId(id);
                poi = hosting;
            } else {
                return null;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return poi;
    }

    /*Constructs Travel object from ResultSet object*/
    public static Travel constructTravelFromRS(ResultSet rs) {
        Travel poi = null;
        int id;
        String name = "";
        String description = "";
        String classification = "";
        String telephone = "";
        String address = "";
        double latitude = 0;
        double longitude = 0;
        boolean ticket = false;
        String type = "";
        double min_price = 0;
        double max_price = 0;
        double avg_price = 0;
        int accomodation = 0;
        String country = "";
        String city = "";
        double price = 0;
        double min_price_night = 0;
        double max_price_night = 0;
        int num_available_rooms = 0;
        boolean lunch_included = false;
        String url = "";
        try {
            //String img_url = "";
            id = rs.getInt(Helper.POIID);
            name = rs.getString(Helper.NAME);
            description = rs.getString(Helper.DESCRIPTION);
            classification = rs.getString(Helper.CLASSIFICATION);
            telephone = rs.getString(Helper.TELEPHONE);
            address = rs.getString(Helper.ADDRESS);
            latitude = rs.getFloat(Helper.LATITUDE);
            longitude = rs.getFloat(Helper.LONGITUDE);
            ticket = rs.getBoolean(Helper.TICKET);
            type = rs.getString(Helper.TYPE);
            min_price = rs.getFloat(Helper.MIN_PRICE);
            max_price = rs.getFloat(Helper.MAX_PRICE);
            avg_price = rs.getFloat(Helper.AVG_PRICE);
            accomodation = rs.getInt(Helper.ACCOMODATION);
            country = rs.getString(Helper.COUNTRY);
            city = rs.getString(Helper.CITY);
            price = rs.getFloat(Helper.PRICE);
            min_price_night = rs.getFloat(Helper.MIN_PRICE_NIGHT);
            max_price_night = rs.getFloat(Helper.MAX_PRICE_NIGHT);
            lunch_included = rs.getBoolean(Helper.LUNCH);
            num_available_rooms = rs.getInt(Helper.NUM_AVAILABLE_ROOMS);
            url = rs.getString(Helper.URL);
            // public Eating (String type,String descr,boolean order, Position pos, double min_price, double max_price,
            // double avg_price, int accomodation)
            //  public Travel (String type,String descr,boolean order, Position pos, String country, String city, double price)
            // public Hosting (String type,String descr,boolean order, Position pos,
            //  double min_price_n,double max_price_n,int num_av_r,boolean lunch)

            if (classification.equalsIgnoreCase(Helper.TRAVEL)) {
                Travel travel = new Travel(type, name, description, address, telephone, ticket, new Position(longitude, latitude), country, city, price);
                travel.setId(id);
                travel.setUrl(url);
                poi = travel;
            } else {
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return poi;
    }
}


